package com.yuliang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuliang.constants.SystemConstants;
import com.yuliang.domain.R;
import com.yuliang.domain.dto.TagDto;
import com.yuliang.domain.dto.UpdateTagDto;
import com.yuliang.domain.entity.Tag;
import com.yuliang.domain.vo.PageVo;
import com.yuliang.domain.vo.TagListVo;
import com.yuliang.domain.vo.TagVo;
import com.yuliang.enums.AppHttpCodeEnum;
import com.yuliang.exception.SystemException;
import com.yuliang.mapper.TagMapper;
import com.yuliang.service.TagService;
import com.yuliang.utils.BeanCopyUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 标签(Tag)表服务实现类
 *
 * @author makejava
 * @since 2023-10-05 19:21:08
 */
@Service("tagService")
public class TagServiceImpl extends ServiceImpl<TagMapper, Tag> implements TagService {

    @Override
    public R<PageVo> pageTagList(Integer pageNum, Integer pageSize, String name, String remark) {
        IPage<Tag> page = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<Tag> queryWrapper = new LambdaQueryWrapper<>();//状态正常，根据name or mark 查询
        queryWrapper.eq(Tag::getDelFlag, SystemConstants.STATUS_NORMAL);
        queryWrapper.like(StringUtils.hasText(name),Tag::getName,name);
        queryWrapper.like(StringUtils.hasText(remark),Tag::getRemark,remark);
        page(page,queryWrapper);

        List<TagVo> tagVos = BeanCopyUtils.copyBeanList(page.getRecords(), TagVo.class);

        return R.okResult(new PageVo(tagVos,page.getTotal()));
    }

    @Override
    public R getListAllTag() {
        List<TagListVo> tagListVos = BeanCopyUtils.copyBeanList(list(), TagListVo.class);
        return R.okResult(tagListVos);
    }

    @Override
    public R addTag(TagDto tagDto) {
        //判空
        if (!StringUtils.hasText(tagDto.getName())){
            throw new SystemException(AppHttpCodeEnum.TAG_NOT_FILL);
        }
        Tag tag = new Tag(tagDto.getName(), tagDto.getRemark());
        //判断重复
        LambdaQueryWrapper<Tag> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Tag::getName,tagDto.getName());
        if (getOne(queryWrapper) != null){
            throw new SystemException(AppHttpCodeEnum.TAG_EXIST);
        }
        //save
        save(tag);
        return R.okResult();
    }

    @Override
    public R deleteTag(Long[] ids) {
        List<Long> list = Arrays.stream(ids)
                .collect(Collectors.toList());
        removeByIds(list);
        return R.okResult();
    }

    @Override
    public R getTagDetail(Long id) {
        LambdaQueryWrapper<Tag> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Tag::getId,id);
        Tag tag = getOne(queryWrapper);
        if (Objects.isNull(tag)){
            throw new SystemException(AppHttpCodeEnum.TAG_NOT_EXIST);
        }
        TagVo tagVo = BeanCopyUtils.copyBean(tag, TagVo.class);
        return R.okResult(tagVo);
    }

    @Override
    public R updateTag(UpdateTagDto updateTagDto) {
        Tag tag = BeanCopyUtils.copyBean(updateTagDto, Tag.class);
        updateById(tag);
        return R.okResult();
    }
}
